package com.zephyr.vchcenter.pbcFile.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class Parser {

	public void parse(File file) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			DefaultHandler defaulthandler = new TestSAXHandler();
			//File f = new File("d:\\111.xml");
			InputStream ins=new FileInputStream(file);
			Reader reader=new InputStreamReader(ins,"utf-8");
			InputSource inso=new InputSource(reader);
			inso.setEncoding("utf-8");
			saxParser.parse(inso, defaulthandler);
			TestSAXHandler handler = (TestSAXHandler) defaulthandler;
			List<String> list = handler.getList();
			System.out.println(list.size()+"---");
			for (String str : list) {
				System.out.println(str);
			}
			System.out.println("�������");
		} catch (Exception e) {
            e.printStackTrace();
		}

	}

	private class TestSAXHandler extends DefaultHandler {
		private List<String> list=new ArrayList<String>();
		private boolean flag=false;
		private StringBuffer content;
        private boolean qnameflag;
        private String tatalName;
		public List<String> getList() {
			return this.list;
		}

		public TestSAXHandler() {
		}

		public void startDocument() throws SAXException {
			list = new ArrayList<String>();
			System.out.println("Event Type: ��ʼ����");
		}

		public void endDocument() throws SAXException {
			System.out.println("Event Type: �������");
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if ("Item".equals(qName)) {
				System.out.println(qName+"---------------===========");
				flag = true;
				
				content = new StringBuffer();
			}
			if(flag&&!"Item".equals(qName)){
				tatalName=qName;
				qnameflag=true;
				
			}
		}
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			// System.out.println("Event Type: End Element");
			if ("Item".equals(qName)){
				System.out.println("/Item");
				list.add(content.toString().trim());
			    flag = false;
			   
			}
		}

		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if(flag&&qnameflag){
				//System.out.println(tatalName+"====");
				//System.out.println(ch);
				String strTemp=new String(ch,start,length).trim();
				if("".equals(strTemp)){
					strTemp="####";
				}
				
				content.append(strTemp).append(":");
				qnameflag=false;
			}
			
		}

		public void error(SAXParseException e) throws SAXException {
			System.out.println("Error: " + e.getMessage());
		}

		public void fatalError(SAXParseException e) throws SAXException {
			System.out.println("Fatal Error: " + e.getMessage());
		}

		public void warning(SAXParseException e) throws SAXException {
			System.out.println("Warning: " + e.getMessage());
		}
	}

	/*public static void main(String[] args) {
		new Parser().parse();
	}*/
}
